package yuan.zk.node;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

/**
 * @version: 1.0
 * @Description: TODO
 * @author: zZmH
 * @date: 2020/09/11 13:45
 */


public class ZKCreate {
    public static final String ip = "192.168.190.128:2181";
    public static final int TIME_OUT = 5000;
    private ZooKeeper zooKeeper;

    @Before
    public void before() throws Exception {
        CountDownLatch latch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(ip, TIME_OUT, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("connection successfully");
                    latch.countDown();
                }
            }

        });
        latch.await();
    }
    @After
    public void after() throws Exception{
        zooKeeper.close();
    }
    //word Authorization
    @Test
    public void create1() throws Exception {
        // arg1:节点的路径
        // arg2:节点的数据
        // arg3:权限列表 world:anyone:cdrwa
        // arg4:节点类型 持久化节点
        zooKeeper.create("/create/node1", "node1".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    //word Authorization
    @Test
    public void create2() throws Exception {
    // arg3:权限列表 world:anyone:r
        zooKeeper.create("/create/node2", "node1".getBytes(),
                ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.PERSISTENT);
    }

    //word Authorization
    @Test
    public void create3()throws Exception{
        ArrayList<ACL> acls = new ArrayList<>();
        Id id = new Id("world", "anyone");
        acls.add(new ACL(ZooDefs.Perms.READ, id));
        acls.add(new ACL(ZooDefs.Perms.WRITE, id));

        // arg3:权限列表 world:anyone
        zooKeeper.create("/create/node3", "node3".getBytes(),
                acls, CreateMode.PERSISTENT);
    }

    //auth Authorization
    @Test
    public void test5()throws Exception{

        zooKeeper.addAuthInfo("digest", "itcast:123456".getBytes());
        zooKeeper.create("/create/node5", "node5".getBytes(),
                ZooDefs.Ids.CREATOR_ALL_ACL, CreateMode.PERSISTENT);

    }
    @Test
    public void test6()throws Exception{

        zooKeeper.addAuthInfo("digest", "itcast:123456".getBytes());
        ArrayList<ACL> acls = new ArrayList<>();
        Id id = new Id("auth", "itcast");
        acls.add(new ACL(ZooDefs.Perms.READ, id));
        zooKeeper.create("/create/node66", "node6".getBytes(),
                acls, CreateMode.PERSISTENT);

    }
    //digeset authorization
    @Test
    public void test7()throws Exception{

        ArrayList<ACL> acls = new ArrayList<>();
        Id id = new Id("digest", "itheima:qlzQzCLKhBROghkooLvb+Mlwv4A=");
        acls.add(new ACL(ZooDefs.Perms.ALL, id));
        zooKeeper.create("/create/node77", "node7".getBytes(),
                acls, CreateMode.PERSISTENT);

    }

    @Test
    //synchronized
    //persistent sequence node
    public void create8() throws Exception {

        String s = zooKeeper.create("/create/node8", "node8".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT_SEQUENTIAL);
        System.out.println(s);
    }

    @Test
    //ephemeral linshide
    public void create9() throws Exception{
        String s = zooKeeper.create("/create/node9", "node9".getBytes(),
                ZooDefs.Ids.READ_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println(s);


    }
    //ephemeral sequence
    @Test
    public void create10() throws Exception{
        String s = zooKeeper.create("/create/node10", "node10".getBytes(),
                ZooDefs.Ids.READ_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(s);
    }

    @Test
    //async
    public void test11()throws Exception {
        zooKeeper.create("/create/node11", "node11".getBytes(),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT,
                new AsyncCallback.StringCallback() {
                    @Override
                    public void processResult(int i, String s, Object o, String s1) {
                        System.out.println(i);
                        System.out.println(s);
                        System.out.println(s1);
                        System.out.println(o);
                    }
                }, "I am ctx");

        Thread.sleep(2000);
        System.out.println("end");
    }


}
